/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * BDIngresosConfigurar.java
 *
 * Created on 06/20/2011, 02:17:39 PM
 */
package org.gingresos.servidoringresos;

import java.sql.Connection;
import java.sql.DriverManager;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JLabel;
import org.gingresosapi.beans.*;
import org.gingresosapi.controller.*;
import org.gingresosapi.generales.Mensajeria;
import org.gingresos.start.MySesion;


/**
 *
 * @author avbravo
 */
public class ServidorIngresosCrear extends javax.swing.JInternalFrame {
    ServidoringresosController servidoringresosController1;
   Servidoringresos servidoringresos1;

    /** Creates new form BDIngresosConfigurar */
    public ServidorIngresosCrear() {
        initComponents();
        servidoringresosController1 = new ServidoringresosController();
        servidoringresos1 = new Servidoringresos();
        
        try{
//             Map<String, String> parametros = new HashMap<String, String>();
//            parametros.put("idmunicipio", SesionLocal.getIdmunicipio());
        servidoringresos1.setIdmunicipio(MySesion.getIdmunicipio());
//          boolean encontrado =  servidoringresosController1.locateByParameters(parametros);
        
//            if (!encontrado) {
//                Mensajeria.Mensaje("Es null");
//            }
        if(!servidoringresosController1.findByIdmunicipio(servidoringresos1)){
             
        }else{
            
        
            if (servidoringresosController1.getServidoringresoslist().size() == 0) {
               // Mensajeria.Mensaje(" No es valido el usuario y el password");
                jButtonGuardar.setEnabled(true);
                jButtonEditar.setEnabled(false);
            } else {
                Servidoringresos servidoringresos = servidoringresosController1.getServidoringresoslist().get(0);
               jTextFieldServidorIngresos.setText(servidoringresos.getServidor());
                 jButtonGuardar.setEnabled(false);
                jButtonEditar.setEnabled(true);
            }
            }
        }catch(Exception ex){
            Mensajeria.MensajeError(ex, "init()");
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jTextFieldServidorIngresos = new javax.swing.JTextField();
        jButtonGuardar = new javax.swing.JButton();
        jButtonCerrar = new javax.swing.JButton();
        jButtonEditar = new javax.swing.JButton();

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);
        setTitle("Servidor Ingresos");

        jLabel2.setText("Servidor");

        jButtonGuardar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/gingresos/resources/gif/save.gif"))); // NOI18N
        jButtonGuardar.setToolTipText("Guardar");
        jButtonGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonGuardarActionPerformed(evt);
            }
        });

        jButtonCerrar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/gingresos/resources/gif/return.png"))); // NOI18N
        jButtonCerrar.setToolTipText("Regresar");
        jButtonCerrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCerrarActionPerformed(evt);
            }
        });

        jButtonEditar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/gingresos/resources/gif/edit.png"))); // NOI18N
        jButtonEditar.setToolTipText("Editar");
        jButtonEditar.setEnabled(false);
        jButtonEditar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonEditarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel2)
                        .addGap(51, 51, 51)
                        .addComponent(jTextFieldServidorIngresos, javax.swing.GroupLayout.PREFERRED_SIZE, 334, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(82, 82, 82)
                        .addComponent(jButtonGuardar, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonEditar, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jButtonCerrar, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(86, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTextFieldServidorIngresos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(48, 48, 48)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonGuardar, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonEditar, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonCerrar, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(40, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonGuardarActionPerformed
        // TODO add your handling code here:
        if (ConectarIngresos()){
             servidoringresos1.setServidor(jTextFieldServidorIngresos.getText());
            servidoringresos1.setIdmunicipio(MySesion.getIdmunicipio());
            servidoringresos1.setUsername(MySesion.getUsername());
            servidoringresos1.setFechausername(MySesion.getFechausername());
           if(servidoringresosController1.insert(servidoringresos1)){
               Mensajeria.Mensaje("Se guardo exitosamente");
           }
           else{
               Mensajeria.Mensaje("No se guardo");
           }
        }
        
    }//GEN-LAST:event_jButtonGuardarActionPerformed

    private void jButtonCerrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCerrarActionPerformed
        // TODO add your handling code here:
        dispose();
    }//GEN-LAST:event_jButtonCerrarActionPerformed

    private void jButtonEditarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonEditarActionPerformed
        // TODO add your handling code here:
         if (ConectarIngresos()){
             servidoringresos1.setServidor(jTextFieldServidorIngresos.getText());
            servidoringresos1.setIdmunicipio(MySesion.getIdmunicipio());
                servidoringresos1.setUsername(MySesion.getUsername());
            servidoringresos1.setFechausername(MySesion.getFechausername());
           if(servidoringresosController1.update(servidoringresos1)){
               Mensajeria.Mensaje("Se edito exitosamente");
           }
           else{
               Mensajeria.Mensaje("No se edito");
           }
        }
    }//GEN-LAST:event_jButtonEditarActionPerformed

    /**
     * @param args the command line arguments
     */
//    public static void main(String args[]) {
//        java.awt.EventQueue.invokeLater(new Runnable() {
//
//            public void run() {
//                new ServidorIngresosCrear().setVisible(true);
//            }
//        });
//    }
//    
     public boolean ConectarIngresos() {
        try {
            String server = jTextFieldServidorIngresos.getText();
            if (server == null || server.equals("")){
                Mensajeria.Mensaje("Indique el servidor de la base de datos de ingresos");
                return false;
            }
            String databaseDB = "sirmupadb";
            String puertoDB = "3306";
            String userDB = "root";
            String passwordDB = "denver";


            String driver = "org.gjt.mm.mysql.Driver";
            Connection con = null;
            Class.forName(driver).newInstance();
            //String url = "jdbc:mysql://" + this.server + "/sirmupadb";
            String url = "jdbc:mysql://" + server + ":"+ puertoDB+"/" + databaseDB;

            //con = DriverManager.getConnection("jdbc:mysql://localhost:3306/sirmupadb", "root", "denver");
            //con = DriverManager.getConnection(url, "root", "denver");

            con = DriverManager.getConnection(url, userDB, passwordDB);

            if (con == null) {
                Mensajeria.Mensaje("No se pudo establecer la conexion ");
           
                return false;
            }
          
            return true;

        } catch (Exception ex) {
           Mensajeria.MensajeError(ex, "Conectar()");
        }
        return false;
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonCerrar;
    private javax.swing.JButton jButtonEditar;
    private javax.swing.JButton jButtonGuardar;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField jTextFieldServidorIngresos;
    // End of variables declaration//GEN-END:variables
}
